package com.qzsoft.zhjd.materiel.modules.materielitem.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.materiel.modules.materielapi.aop.MaterielApiUpdate;
import com.qzsoft.zhjd.materiel.modules.materielitem.dto.MaterielItemListDto;
import com.qzsoft.zhjd.materiel.modules.materielitem.dto.MaterielItemTreeDto;
import com.qzsoft.zhjd.materiel.modules.materielitem.entity.MaterielItemEntity;
import com.qzsoft.zhjd.materiel.modules.materielitem.service.MaterielItemService;

/**
 * @ClassName MaterielItemController
 * @Description 数据字典控制器
 * @author linhuawei
 * @since 2022-03-29
 */
@RestController
@RequestMapping("/api/materielItem")
public class MaterielItemController {

    @Autowired
    private MaterielItemService materielItemService;

    /**
     * @Title categoryTreeDto
     * @Description 获取材料类型树
     * @return 材料类型树
     */
    @RequiresPermissions("materiel:item:page")
    @GetMapping("/categoryTree")
    public Result<List<MaterielItemTreeDto>> categoryTree() {
        List<MaterielItemTreeDto> data = materielItemService.categoryTreeDto();
        return ResultUtils.ok(data);
    }

    /**
     * @Title itemPage
     * @Description 获取材料分页列表
     * @param params 查询条件
     * @return 材料分页列表
     */
    @RequiresPermissions("materiel:item:page")
    @GetMapping("/itemPage")
    public Result<IPage<MaterielItemListDto>> itemPage(@RequestParam Map<String, Object> params) {
        IPage<MaterielItemListDto> data = materielItemService.itemPage(params);
        return ResultUtils.ok(data);
    }

//    /**
//     * 访问url获取、存储材料字典
//     * @Author wang_hyu
//     * @date :2022年9月23日
//     */
//    @GetMapping("/urlSaveInfo")
//    @RequiresPermissions("materiel:item:page")
//    public Result<String> getUrlInfo() throws IOException {
//        materielItemService.getUrlInfo();
//        Result<String> results = ResultUtils.ok();
//        return results;
//    }


    /**
     * @Title info
     * @Description 获取数据对象
     * @param id 数据主键
     * @return 数据对象
     */
    @RequiresPermissions("materiel:item:update")
    @GetMapping("/info/{id}")
    public Result<MaterielItemEntity> info(@PathVariable("id") String id) {
        MaterielItemEntity entity = materielItemService.getById(id);
        if (null == entity) {
            RenExceptionUtils.throwEx(BizCode.DATA_NOT_FOUND, "数据不存在");
        }
        return ResultUtils.ok(entity);
    }

    /**
     * @Title save
     * @Description 添加数据
     * @param entity 需要添加的数据
     * @return 添加结果
     */
    @MaterielApiUpdate("getItemList")
    @RequiresPermissions("materiel:item:save")
    @PostMapping("/save")
    public Result<String> save(@RequestBody MaterielItemEntity entity) {
        ValidatorUtils.validateEntity(entity, AddGroup.class);
        materielItemService.save(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title update
     * @Description 修改数据
     * @param entity 需要修改的数据
     * @return 修改结果
     */
    @MaterielApiUpdate("getItemList")
    @RequiresPermissions("materiel:item:update")
    @PostMapping("/update")
    public Result<String> update(@RequestBody MaterielItemEntity entity) {
        ValidatorUtils.validateEntity(entity, UpdateGroup.class);
        materielItemService.updateById(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title delete
     * @Description 删除数据
     * @param id 数据主键
     * @return 删除结果
     */
    @MaterielApiUpdate("getItemList")
    @RequiresPermissions("materiel:item:delete")
    @PostMapping("/delete/{id}")
    public Result<String> delete(@PathVariable("id") String id) {
        materielItemService.removeById(id);
        return ResultUtils.ok();
    }

    /**
     * @Title weighedTree
     * @Description 获取称重材料类型树
     * @return 称重材料类型树
     */
    @GetMapping("/weighedTree")
    public Result<List<MaterielItemTreeDto>> weighedTree() {
        List<MaterielItemTreeDto> data = materielItemService.weighedTree();
        return ResultUtils.ok(data);
    }

}
